import axios from 'axios';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import { getUserTempId } from '../utils/uuid';
import store from '../store/index';

const ajax = axios.create({
    baseURL: 'http://39.98.123.211',
    timeout: 5000,
});

//设置请求拦截器
ajax.interceptors.request.use(config => {
    NProgress.start();

    //使用uuid
    config.headers.userTempId = getUserTempId();

    const token = store.state.userlogin.userInfo.token;

    if (token) {
        config.headers.token = token;
    }

    return config; // 返回配置项 这个config里面包含请求的一些配置信息 url method headers
});

//设置响应拦截器
ajax.interceptors.response.use(
    response => {
        NProgress.done();
        return response.data;
    },
    err => {
        NProgress.done();
        return Promise.reject(new Error(Error.message));
    }
);

export default ajax;
